from docx import Document

# document = Document()

input_doc = 'input.docx'
document = Document(input_doc)

# --- 插入图片到指定位置
context_dc = {
    'signature_picture': 123
}

pic = "temp/a1.jpg"
# run = paragraph.add_run()
# run.add_picture(pic, width = 1400000, height = 1400000)
from docx.shared import Inches

document.add_picture(pic, width=Inches(1.0), height=Inches(1.0))

# tables = document.tables

if 0:
    paragraph = document.add_paragraph('这是个段落。')
    prior_paragraph = paragraph.insert_paragraph_before('这是前面的段落。')
    document.add_heading('这是个标题')
    document.add_heading('The role of dolphins', level=2)

    table = document.add_table(rows=2, cols=2)
    cell = table.cell(0, 1)
    cell.text = '这是第一行第二列的单元格'

    row = table.rows[1]
    row.cells[0].text = '第二行第一列'
    row.cells[1].text = '第二行第二列'

    for row in table.rows:
        for cell in row.cells:
            print(cell.text)

    row_count = len(table.rows)
    col_count = len(table.columns)
    # row = table.add_row()

output_doc = 'output_doc.docx'
document.save(output_doc)


# --------- 转化为html
from pydocx import PyDocX
html = PyDocX.to_html(output_doc)

output_html = 'output_doc.html'
f = open(output_html, 'w', encoding="utf-8")
f.write(html)
f.close()

import os
os.system('open ' + output_doc)
# os.open(output_html)

